var express = require('express');
var router = express.Router();
var http = require('http');

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
/* 轮播数据 */
router.get('/lunb', function(req, res) {
   
    var t = req.query.time;

    var url = 'http://m.maizuo.com/v4/api/billboard/home?__t=' + t;

    http.get(url, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })

        response.on('end', function() {
            res.send(data);
        })
    });
})

/* 热映电影数据 */
router.get('/rebo', function(req, res) {
   
    var t = req.query.time;

    var url1 = 'http://m.maizuo.com/v4/api/film/now-playing?__t=' + t+'&page=1&count=5';

    http.get(url1, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })

        response.on('end', function() {
            res.send(data);
        })
    });
})
/* 即将上映电影数据*/
router.get('/say', function(req, res) {
   
    var t = req.query.time;

    var url2 = 'http://m.maizuo.com/v4/api/film/coming-soon?__t' + t+'&page=1&count=3';

    http.get(url2, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })

        response.on('end', function() {
            res.send(data);
        })
    });
})

/*详情*/
router.get(/\d$/, function(req, res) {
    var t = req.query.time;
    console.log('=============');
    var id = url.parse(req.url).pathname.substr(1);
    console.log(id);
    console.log('=============');

   

    var url1 = "http://m.maizuo.com/v4/api/film/" +id+ "?__t=" + t;
    console.log(url);
    
    http.get(url1, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    });
    
})
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
